/*******************************************************************************
 * FilePath    : /Software/Examples_Adv_MS1656/adv_app_TxRx/Src/BSP/Key.c
 * Version     : 1.0
 * Brief       : 
 * Author      : AndrewHu
 * Company     : Shanghai MacroGiga Electronics CO.,Ltd
 * Email       : Hubinbo@macrogiga.com
 * Date        : 2020-03-24 15:42:28
 * LastEditors : AndrewHu
 * LastEditTime: 2021-08-10 10:13:24
 * Description : 
 ******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include <Includes.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define keyDelay_Set 0x05 //5ms*5
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
u8 keyFlag = 0x00;
u8 keyDelay = 0x00;
/* Private function ----------------------------------------------------------*/
/* extern --------------------------------------------------------------------*/
/*******************************************************************************
 * Function    : KEY_GPIO_Init
 * Brief       : 
 * Parameter   : 
 * Returns     : 
 * Date        : 2020-03-24 17:09:29
 * Description : 
 ******************************************************************************/
static void KEY_GPIO_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    __HAL_RCC_GPIOC_CLK_ENABLE();

    GPIO_InitStruct.Pin = GPIO_PIN_3;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.OpenDrain = GPIO_PUSHPULL;
    GPIO_InitStruct.Debounce.Enable = GPIO_DEBOUNCE_DISABLE;
    GPIO_InitStruct.SlewRate = GPIO_SLEW_RATE_HIGH;
    GPIO_InitStruct.DrvStrength = GPIO_DRV_STRENGTH_HIGH;
    GPIO_InitStruct.Pull = GPIO_PULLDOWN;
    GPIO_InitStruct.Alternate = 0;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}
static u8 IsKeyStatus(void)
{
    return HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_3);
}
/*******************************************************************************
 * Function    : KEY_Init
 * Brief       : 
 * Parameter   : 
 * Returns     : 
 * Date        : 2020-03-24 16:28:55
 * Description : 按键初始化
 ******************************************************************************/
void KEY_Init(void)
{
    KEY_GPIO_Init();
}
/*******************************************************************************
 * Function    : 
 * Brief       : 
 * Parameter   : 
 * Returns     : 
 * Date        : 2020-03-25 16:57:53
 * Description : 按键扫描
 ******************************************************************************/
void KEY_Scan(void)
{
    static u8 status = 1;

    if (IsKeyStatus())
    {
        if (keyFlag != 0x00)
            keyDelay = 0x00;
    }
    else
    {
        if (keyFlag == 0x00)
            keyDelay = 0x00;
    }

    if (keyDelay < keyDelay_Set)
    {
        keyDelay++;
        return;
    }
    //按键状态有改变
    keyDelay = 0x00;
    if (keyFlag)
    {
        keyFlag = 0x00;
    }
    else
    {
        keyFlag++;
        //按键按下

        //接收数据显示功能切换，串口输出
        if (BLE_GetRxDisplay())
            BLE_SetRxDisplay(0);
        else
            BLE_SetRxDisplay(1);
    }
}
